﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LCL.LogNet
{
    /*************************************************************************************
  *    目标：
  *        1. 高性能的日志类
  *        2. 灵活的配置
  *        3. 日志分级
  *        4. 控制输出
  *        5. 方便筛选
  *        6. 方便的配置按小时，天，月，年记录
  *************************************************************************************/
    /// <summary>
    /// 日志类的管理器
    /// </summary>
    internal class LogNetManagment
    {
        internal static string LogNetDebug = "调试";
        internal static string LogNetInfo = "信息";
        internal static string LogNetWarn = "警告";
        internal static string LogNetError = "错误";
        internal static string LogNetFatal = "致命";
        internal static string LogNetAbandon = "放弃";
        internal static string LogNetAll = "全部";

        internal static string ExceptionMessage = "错误信息：";
        internal static string ExceptionSourse = "错误源：";
        internal static string ExceptionType = "错误类型：";
        internal static string ExceptionStackTrace = "错误堆栈：";
        internal static string ExceptopnTargetSite = "错误方法：";
        internal static string ExceprionCustomer = "用户自定义方法出错：";

        internal const int LogSaveModeBySingleFile = 1;
        internal const int LogSaveModeByFileSize = 2;
        internal const int LogSaveModeByDateTime = 3;

        internal const string LogFileHeadString = "Logs_";
        internal static string GetDegreeDescription(LogLevel degree)
        {
            switch (degree)
            {
                case LogLevel.DEBUG: return LogNetDebug;
                case LogLevel.INFO: return LogNetInfo;
                case LogLevel.WARN: return LogNetWarn;
                case LogLevel.ERROR: return LogNetError;
                case LogLevel.FATAL: return LogNetFatal;
                case LogLevel.None: return LogNetAbandon;
                default: return LogNetAbandon;
            }
        }
      
        /// <summary>
        /// 公开的一个静态变量，允许随意的设置
        /// </summary>
        public static ILogNet LogNet { get; set; }
        /// <summary>
        /// 通过异常文本格式化成字符串用于保存或发送
        /// </summary>
        /// <param name="text">文本消息</param>
        /// <param name="ex">异常</param>
        /// <returns>异常最终信息</returns>
        public static string GetSaveStringFromException(string text, Exception ex)
        {
            StringBuilder builder = new StringBuilder(text);
            if (ex != null)
            {
                if (!string.IsNullOrEmpty(text))
                {
                    builder.Append(" : ");
                }
                try
                {
                    builder.Append(ExceptionMessage);
                    builder.Append(ex.Message);
                    builder.Append(Environment.NewLine);
                    builder.Append(ExceptionSourse);
                    builder.Append(ex.Source);
                    builder.Append(Environment.NewLine);
                    builder.Append(ExceptionStackTrace);
                    builder.Append(ex.StackTrace);
                    builder.Append(Environment.NewLine);
                    builder.Append(ExceptionType);
                    builder.Append(ex.GetType().ToString());
                    builder.Append(Environment.NewLine);
                    builder.Append(ExceptopnTargetSite);
                    builder.Append(ex.TargetSite.ToString());
                }
                catch
                {
                }
                builder.Append(Environment.NewLine);
                builder.Append("\u0002/=================================================[    Exception    ]================================================/");
            }
            return builder.ToString();
        }

    }
}
